package com.fushenlan.epidemicControl.controller;

import cn.hutool.core.util.StrUtil;
import com.fushenlan.Vo.NeighborhoodIndoVo;
import com.fushenlan.commons.ApiGlobalErrorDefinition;
import com.fushenlan.commons.GeneralResponse;
import com.fushenlan.commons.ResultUtils;
import com.fushenlan.epidemicControl.dto.NeighborhoodInfoDto;
import com.fushenlan.epidemicControl.service.NeighborhoodInfoService;
import com.fushenlan.po.NeighborhoodInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author XUJIN
 * @DATE 2022/4/21
 * @PACKAGE_NAME com.fushenlan.epidemicControl.controller
 * @VERSION 1.0
 * 居委会信息管理控制器
 */
@Slf4j
@RestController
@RequestMapping("neighbor")
@Api(value = "居委会信息管理控制器", description = "居委会信息管理控制器", tags = "neighborhood-controller")
public class NeighborhoodInfoController {

    @Autowired
    private NeighborhoodInfoService neighborhoodInfoService;

    @GetMapping("getNeighborList")
    @ApiOperation(value = "获取居委会列表", notes = "获取居委会列表")
    public GeneralResponse getNeighborList() {
        List<NeighborhoodInfo> neighborList = neighborhoodInfoService.findNeighborList(new NeighborhoodInfo());
        //List<NeighborhoodIndoVo> neighborListByPage = neighborhoodInfoService.getNeighborListByPage(pageNo, pageSize, neighborList);
        return GeneralResponse.success(neighborList);
    }

    @PostMapping("/addNeighborhoodInfo")
    @ApiOperation(value = "添加居委会信息", notes = "添加居委会信息")
    public GeneralResponse addNeighborhoodInfo(NeighborhoodInfoDto neighborhoodInfoDto) {
        int row = 0;
        try {
            List<NeighborhoodIndoVo> neighborList = neighborhoodInfoService.getNeighborList(neighborhoodInfoDto);
            String pcode = neighborhoodInfoDto.getStreetCode();
            String name = neighborhoodInfoDto.getNeighborhoodName();
            //校验居委名称是否已存在，同一街道不能有同名的居委名称
            for (NeighborhoodIndoVo neighborhoodIndoVo : neighborList){
                if (StrUtil.equals(pcode,neighborhoodIndoVo.getStreetCode()) && StrUtil.equals(name,neighborhoodIndoVo.getNeighborhoodName())){
                   return GeneralResponse.fail(ApiGlobalErrorDefinition.NEIGHBORHOOD_REPEATED);
                }
            }
            row = neighborhoodInfoService.addNeighbor(neighborhoodInfoDto);
        } catch (Exception e) {
           log.error("服务器异常",e);
           return GeneralResponse.fail();
        }
        return row > 0 ? GeneralResponse.success() : GeneralResponse.fail();
    }

    @PostMapping("/deleteNeighbor")
    @ApiOperation(value = "删除居委会", notes = "删除居委会")
    public GeneralResponse deleteNeighbor(@ApiParam(value = "居委会主键id", required = true) @RequestParam  Integer id) {

        if (id == null){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.NOT_SELECT);
        }
        int row = 0;
        try {
            row = neighborhoodInfoService.deleteNeighbor(id);
        } catch (Exception e) {
            log.error("服务器异常",e);
            return GeneralResponse.fail();
        }
        return row > 0 ? GeneralResponse.success() : GeneralResponse.fail();
    }

    @PostMapping("/updateNeighbor")
    @ApiOperation(value = "更新居委会信息", notes = "更新居委会信息")
    public GeneralResponse updateNeighbor(NeighborhoodInfoDto neighborhoodInfoDto) {
        int row = 0;
        ResultUtils resultUtils = new ResultUtils();
        try {
            row = neighborhoodInfoService.updateNeighbor(neighborhoodInfoDto);
        } catch (Exception e) {
            log.error("服务器异常",e);
            return GeneralResponse.fail();
        }
        return row > 0 ? GeneralResponse.success() : GeneralResponse.fail();
    }


}
